﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cosmetic.Logic;
using Cosmetic.DataEntity;
using System.Collections;
using System.Data;

namespace WebApp.Management.Product
{
    public partial class ProductDiscountManage : PageBase2
    {
        private ProductManager m_ProductManager = new ProductManager();
        private CommentManager m_CommentManager = new CommentManager();
        private BrandManager m_BrandManager = new BrandManager();
        private ProductSeriesManager m_SeriesManager = new ProductSeriesManager();
        private CategoryManager m_CategoryManager = new CategoryManager();
        SaleManager m_salemanager = new SaleManager();

        private RoleManager m_RoleManager = new RoleManager();

        private Order m_Order;

        private Cosmetic.DataEntity.Category m_Category = new Category();
        private Cosmetic.DataEntity.Category SaveFrontCategory = new Category();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.SignInUser1 == null)
            {
                Session.Remove("SignInUser1");
                this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('请先登录权限!');window.top.location.href='/Management/indexs.aspx';", true);
                return;
            }
            if (!IsPostBack)
            {
                Cosmetic.DataEntity.User theUser = this.SignInUser1;
                if (!this.m_RoleManager.CheckUserRole(theUser, RoleManager.ERole.Admin))
                {
                    Session.Remove("SignInUser1");
                    this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('你没有管理员权限!');window.top.location.href='/Management/indexs.aspx';", true);
                    return;
                }
            }
        }

        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //string theCategoryId = this.Request.QueryString["CID"];
                //if (!string.IsNullOrEmpty(this.Request.QueryString["CID"]))
                //{
                //    lvProdust.DataSource = this.m_ProductManager.GetProductByCategory(new Guid(this.Request.QueryString["CID"]));
                //    lvProdust.DataBind();
                //}
            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            BindProduct();
            BindDiscountProduct();
        }
        List<Cosmetic.DataEntity.Product> theProductList = null;
        private void BindProduct()
        {
            theProductList = new List<Cosmetic.DataEntity.Product>();
            if (this.txtProductCode.Text.Trim() != string.Empty)
            {
                theProductList = this.m_ProductManager.GetProductByProductCode(this.txtProductCode.Text.Trim());
                this.txtProductCode.Text = string.Empty;
            }
            else
            {
                theProductList = this.m_ProductManager.GetAll().OrderByDescending(theProduct => theProduct.CreateTime).ToList(); ;
            }
            this.lvProdust.DataSource = theProductList;
            this.lvProdust.DataBind();
        }

        protected void lvProdust_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                Cosmetic.DataEntity.Product theProduct = this.m_ProductManager.GetProductById(new Guid(e.CommandArgument.ToString()));
                if (e.CommandName.Equals("OnSelect"))
                {
                    this.txtProductName.Text = theProduct.Name;
                    this.HFPID.Value = e.CommandArgument.ToString();
                }
            }
        }

        protected void btnShow_Click(object sender, EventArgs e)
        {
            List<Cosmetic.DataEntity.Product> theProductList = new List<Cosmetic.DataEntity.Product>();
            if (this.txtProductCode.Text.Trim() != string.Empty)
            {
                theProductList = this.m_ProductManager.GetProductByProductCode(this.txtProductCode.Text.Trim());
            }
            this.lvProdust.DataSource = theProductList;
            this.lvProdust.DataBind();
        }

        protected void lvProdust_PagePropertiesChanged(object sender, EventArgs e)
        {
            this.BindProduct();
        }

        DiscountProductManagement discountPM = new DiscountProductManagement();

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtPrice.Text))
            {
                return;
            }
            Cosmetic.DataEntity.ProductDiscount pro = new ProductDiscount();
            pro.ProductId = new Guid(this.HFPID.Value);
            pro.discountPrice = decimal.Parse(this.txtPrice.Text);
            discountPM.InsertProduct(pro);
            BindDiscountProduct();

        }

        protected void BindDiscountProduct()
        {
            this.lvProductnew.DataSource = discountPM.GetAllProduct();
            this.lvProductnew.DataBind();
        }

        protected void lvProductnew_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                if (e.CommandName == "onDelete")
                {
                    discountPM.DeleteProduct(new Guid(e.CommandArgument.ToString()));
                }
                BindDiscountProduct();
            }
        }

        protected void lvProductnew_PagePropertiesChanged(object sender, EventArgs e)
        {
            BindDiscountProduct();
        }

     
    }//end class
}